package com.icangya.DAO.impl;

import com.icangya.DAO.ICommissionRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.CommissionRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Queue;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;
import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;

@Repository
public class CommissionRecordDAOImpl  extends BaseDAO<CommissionRecord> implements ICommissionRecordDAO{

    @Override
    public List getCommissionRecordByOpenId(String openId) {
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql=" FROM CommissionRecord cr where cr.openId='"+openId+"'order by cr.commissionTime desc";
        Query query=session.createQuery(hql);
//       query.setMaxResults(10);
       List list=query.list();
//        String count = "SELECT COUNT(cr) " + hql;
//        Long total = (Long) getHibernateTemplate().find(count).get(0);
//        List list= SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, 1, 10);
       // String hql="FROM CommissionRecord cr where cr.openId=? order by cr.commissionTime desc limit 10";
        if(list !=null)
            return  list;
        return null;
       // return getHibernateTemplate().find(hql,openId);
    }

    @Override
    public List getCommRecordByOpenIdOrCommType(String openId, String commissionType) {
        String hql="FROM CommissionRecord cr where cr.openId=? and cr.commissionType=?";

        return getHibernateTemplate().find(hql,openId,commissionType);
    }

    @Override
    public List getCommRecordByOpenIdOrCommTypec(long userid, int commissionTypec) {

        String hql=" FROM CommissionRecord cr where cr.userid=? and cr.commissionTypec=?";

        return getHibernateTemplate().find(hql,userid,commissionTypec);

    }

    @Override
    public List getCommRecordByOpenId(int startPage,String openId) {

        String hql=" FROM CommissionRecord cr where cr.openId='"+openId+"' order by cr.commissionTime desc";
            String count = "SELECT COUNT(cr) " + hql;
            Long total = (Long) getHibernateTemplate().find(count).get(0);

        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, FOOT_PAGE_ROW);

    }

    @Override
    public Double getProxyCommMoney(String openId) {
        String hql="SELECT sum(cr.commissionMoney) from CommissionRecord cr where cr.commissionType='好友奖励' and cr.openId='"+openId+"'";
        Double sumMoney = (Double) getHibernateTemplate().find(hql).get(0);
        if(sumMoney==null){
            return 0.0;
        }
        return sumMoney;
    }
}
